package com.smzd.projforge.actions;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.core.actions.AbstractScopedController;
import com.smzd.core.annotations.Access;
import com.smzd.core.business.AbstractService;
import com.smzd.core.utils.db.SRPage;
import com.smzd.projforge.business.DynaAttributeService;
import com.smzd.projforge.business.DynaClassService;
import com.smzd.projforge.domain.DynaAttribute;
import com.smzd.projforge.domain.DynaClass;

@Controller
@Access
@RequestMapping("/class/{scopeId}/attributes")
public class DynaAttributeController extends
		AbstractScopedController<DynaAttribute, DynaClass> {
	@Autowired
	private DynaAttributeService service;
	@Autowired
	private DynaClassService classService;

	@Override
	protected AbstractService<DynaAttribute> getService() {
		return service;
	}

	@Override
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String toAddPage(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaAttribute elem,
			BindingResult result) throws Exception {
		return super.toAddPage(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String postAddData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaAttribute elem,
			BindingResult result) throws Exception {
		if (elem.getType() != DynaAttribute.TYPE_DYNACLASS) {
			elem.setRefClass(null);
		} else {
			if (elem.getRefClass() != null
					&& elem.getRefClass().getId() != null
					&& elem.getRefClass().getId().intValue() == 0) {
				elem.setRefClass(null);
			}
		}
		return super.postAddData(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(method = RequestMethod.GET)
	public String toListPage(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaAttribute elem,
			BindingResult result) throws Exception {
		return super.toListPage(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public String getListData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId,
			SRPage<DynaClass, DynaAttribute> paging, BindingResult result)
			throws Exception {
		return super.getListData(model, scopeId, paging, result);
	}

	@Override
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String toEditPage(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaAttribute elem,
			BindingResult result) throws Exception {
		elem = service.getItemById(elem.getId());
		return super.toEditPage(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public String postEditData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaAttribute elem,
			BindingResult result) throws Exception {
		if (elem.getType() != DynaAttribute.TYPE_DYNACLASS) {
			elem.setRefClass(null);
		} else {
			if (elem.getRefClass() != null
					&& elem.getRefClass().getId() != null
					&& elem.getRefClass().getId().intValue() == 0) {
				elem.setRefClass(null);
			}
		}
		return super.postEditData(model, scopeId, elem, result);
	}

	@Override
	@RequestMapping(value = "/remove")
	@ResponseBody
	public String postRemoveData(Map<String, Object> model,
			@PathVariable("scopeId") int scopeId, DynaAttribute elem,
			BindingResult result) throws Exception {
		return super.postRemoveData(model, scopeId, elem, result);
	}

	@Override
	protected DynaClass getScope(int scopeId) {
		return classService.getItemById(scopeId);
	}

	@RequestMapping(value="/mtm")
	@ResponseBody
	public String getMTMAttributeCandidates(@PathVariable("scopeId") int scopeId){
		return service.getMTMAttributeCandidates(scopeId);
	}
}
